﻿using Kingdee.BOS.Authentication;
using Kingdee.BOS.Client.Core;
using Kingdee.BOS.ServiceFacade.KDServiceClient.Account;
using System;
using System.Windows;
using System.Windows.Controls;

namespace Caraya.XUI.LoginPlugIn
{
    /// <summary>
    /// CustomLoginUI.xaml 的交互逻辑
    /// </summary>
    public partial class CustomLoginUI : UserControl, IKDLoginControl
    {
        IKDLoginControlOwner Owner { get; set; }
        ItemModel currentDataCenter { get; set; }
        public CustomLoginUI()
        {
            InitializeComponent();
            this.Dispatcher.BeginInvoke(new Action(() => InitDataCenterList()));
        }
        private void InitDataCenterList()
        {
            CboDCName.DisplayMemberPath = "Name";
            CboDCName.SelectedValuePath = "Id";
            var proxy = new AccountClientProxy();
            var dclist = proxy.GetDataCenterList();
            CboDCName.ItemsSource = dclist;
            CboDCName.SelectedIndex = 0;
            TxtUserName.Text = "宋小康";
            TxtPassword.Password = "123456";
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(string.Format("userName:{0}\r\npassword:{1}", TxtUserName.Text, TxtPassword.Text));
            var sDCName = CboDCName.Text;
            var ret = this.Owner.DoLogin();
            ret.Context.DataCenterName = sDCName;
            if (ret.LoginResultType == LoginResultType.Success)
            {
                this.Owner.LoadMainWindow();
            }
            else
            {
                this.Owner.ShowErrorMessage(new Exception(ret.Message));
            }
        }

        public void ClearData()
        {
            //
        }

        public void DoLogin()
        {
            //
        }

        public bool Focus(string key)
        {
            return true;
        }

        public string GetFormTitle()
        {
            return "ZQWLoginV1";
        }

        public LoginInfo GetLoginInfo()
        {
            var acctID = CboDCName.SelectedValue as string;
            string userName = this.TxtUserName.Text.Trim();
            string userPwd = this.TxtPassword.Password.Trim();
            int lcid = 2052;
            var loginInfo = new LoginInfo()
            {
                Username = userName,
                Password = userPwd,
                AcctID = acctID,
                Lcid = lcid,
                AuthenticateType = AuthenticationType.PwdAuthentication,
                ClientInfo = this.GetOwner().GetClientInfo()
            };
            return loginInfo;
        }

        public IKDLoginControlOwner GetOwner()
        {
            return this.Owner;
        }

        public KDSize GetUISize()
        {
            return new KDSize() { Height = 500, Width = 700 };
        }

        public bool IsDataLoaded()
        {
            return true;
        }

        public void LoadLocalSettings()
        {
            //
        }

        public IKDLoginControl NewInstance()
        {
            return new CustomLoginUI();
        }

        public void Release()
        {
            this.Owner = null;
        }

        public void SaveLoginSettings()
        {
            //
        }

        public void SetOwner(IKDLoginControlOwner owner)
        {
            this.Owner = owner;
        }

        public void SetProgressTips(string tips)
        {
            //throw new NotImplementedException();
        }

        public void UpdateGobalLocalId(string localId)
        {
            //throw new NotImplementedException();
        }
    }
}
